package com.cljz.makepassenger.ui.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.event.UserInfoChangeEvent;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.present.activity.PhoneBindingP;
import com.cljz.makepassenger.utils.CommonUtils;
import com.cljz.makepassenger.utils.L;
import com.cljz.makepassenger.utils.T;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by Administrator on 2017/4/7 0007.
 */

public class PhoneBindingActivity extends BaseActivity<PhoneBindingP> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.txt_now_bind_phone)
    TextView txtNowBindPhone;
    @BindView(R.id.edt_new_phone)
    EditText edtNewPhone;
    @BindView(R.id.btn_get_sms_code)
    Button btnGetSmsCode;
    @BindView(R.id.edt_input_sms_code)
    EditText edtInputSmsCode;
    @BindView(R.id.tv_timecount)
    TextView tvTimeCount;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.fl_main)
    FrameLayout flMain;
    private TimeCount timeCount;


    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("绑定手机");
        tvTitleRight.setText("提交");
        tvTitleRight.setTextSize(16);
        txtNowBindPhone.setText(getP().getLocalUserInfo(getP().getToken()).getUser_phone());
        timeCount = new TimeCount(60000, 1000);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting_phone_binding;
    }

    @Override
    public PhoneBindingP newP() {
        return new PhoneBindingP();
    }

    @Override
    protected void initListener() {
        super.initListener();
        //订阅事件分发
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(1, TimeUnit.SECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                finish();
            }
        });
        RxTextView.textChanges(edtNewPhone)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    //CharSequence可读可写序列
                    @Override
                    public void call(CharSequence charSequence) {

                    }
                });
        //点击获取验证码的按钮
        RxView.clicks(btnGetSmsCode)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (edtNewPhone.getText().length() == 0) {
                            T.getInstance().showShort("请输入要绑定的手机号");
                        } else if (!CommonUtils.checkPhone(edtNewPhone.getText().toString())) {
                            T.getInstance().showShort("请输入正确格式的手机号");
                        } else {
                            //可以获取到验证码
//                    bundle.putString("phone", edtNewPhone.getText().toString());

                            getP().getSms();
                            L.e("msg", "获取到了验证码");
                        }
                    }
                });
        //进行点击来更换绑定的号码
        RxView.clicks(tvTitleRight)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(3, TimeUnit.SECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                if (CommonUtils.isNetworkAvailable()) {
                    showLoadingDialog();
                    getP().changeBySms(edtNewPhone.getText().toString().trim(), edtInputSmsCode.getText().toString());
                } else {
                    T.getInstance().showShort("请开启网络");
                }
            }
        });
    }

    public void getSmsSucceed() {
        dismissLoadingDialog();
        timeCount.start();
    }

    public void getSmsFail(String err) {
        dismissLoadingDialog();
        T.getInstance().showShort(err);
    }


    private class TimeCount extends CountDownTimer {

        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {
            tvTimeCount.setVisibility(View.VISIBLE);
            tvTimeCount.setText("请" + l / 1000 + "s后再进行验证码获取");
        }

        @Override
        public void onFinish() {
            tvTimeCount.setVisibility(View.GONE);
        }
    }


    public void changeSucceed(ResponseBean data) {
        dismissLoadingDialog();
        T.getInstance().showShort(data.getMsg());
        BusProvider.getBus().post(new UserInfoChangeEvent());
        Router.pop(this);

    }

    public void changeFail(String s) {
        dismissLoadingDialog();
        T.getInstance().showShort(s);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timeCount.cancel();
        timeCount = null;
    }

}
